package com.signway.javaui.slice.component;

import com.signway.javaui.ResourceTable;
import com.signway.javaui.base.BaseAbilitySlice;
import com.signway.javaui.utils.SwLog;
import com.signway.javaui.utils.SwToast;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.text.Font;

public class TextComponent extends BaseAbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_component_text);

        initView();
    }

    private void initView() {
        /* 上下页切换 */
        DirectionalLayout firstPage = (DirectionalLayout) findComponentById(ResourceTable.Id_first_page);
        DirectionalLayout secondPage = (DirectionalLayout) findComponentById(ResourceTable.Id_second_page);
        Button btnSwitch = (Button) findComponentById(ResourceTable.Id_next_page_button);
        btnSwitch.setTag(true);
        btnSwitch.setClickedListener(component -> {
            boolean flag = (boolean) component.getTag();
            if (flag) {/* 切换下一页 */
                btnSwitch.setTag(false);
                firstPage.setVisibility(Component.HIDE);
                secondPage.setVisibility(Component.VISIBLE);
                btnSwitch.setText("上一页");
            } else  {/* 切换上一页 */
                btnSwitch.setTag(true);
                firstPage.setVisibility(Component.VISIBLE);
                secondPage.setVisibility(Component.HIDE);
                btnSwitch.setText("下一页");
            }
        });

        Text textBold = (Text) findComponentById(ResourceTable.Id_text_bold);
        textBold.setFont(Font.DEFAULT_BOLD);/* 文本加粗 */

        /* 文本框 */
        TextField inputId = (TextField) findComponentById(ResourceTable.Id_text_field_id);
        inputId.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEARCH);
        inputId.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                SwLog.info("文本框发生变化" + s);
            }
        });

        /* 计时器 */
        TickTimer tickTimer = (TickTimer) findComponentById(ResourceTable.Id_tick_timer);
        tickTimer.setBaseTime(0);
        tickTimer.setFormat("yyyy:MM:dd HH:mm:ss");
        tickTimer.start();

        TickTimer tickTimerCountDown = (TickTimer) findComponentById(ResourceTable.Id_tick_timer_count_down);
        tickTimerCountDown.setFormat("yyyy:MM:dd HH:mm:ss");
        tickTimerCountDown.setTickListener(new TickTimer.TickListener() {
            @Override
            public void onTickTimerUpdate(TickTimer tickTimer) {
                long startTime = (long) tickTimer.getTag();
                if (System.currentTimeMillis() - startTime >= 5 * 1000) {
                    SwToast.showToast(BaseAbilitySlice.getTopAbilitySlice(), "计时结束");
                    tickTimer.stop();
                }
            }
        });

        Button startTickTimer = (Button) findComponentById(ResourceTable.Id_start_tick_timer);
        startTickTimer.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                tickTimerCountDown.start();
                tickTimerCountDown.setBaseTime(System.currentTimeMillis() + 5 * 1000);
                tickTimerCountDown.setTag(System.currentTimeMillis());
            }
        });
    }
}
